import * as bajaService from '@/services/bajaService';
import { CORE_JAR_PREFIX } from '@/utils/constants';
import memoryUtils from '@/utils/memoryUtils';

export default {
  namespace: 'subModule',
  state: {
    $handle: '',
    $typeSpec: CORE_JAR_PREFIX + ':CommonModule',
    title: '',
    nIcon: '',
    moduleType: 'Tree Menu',
    pagePath: '/subSystem',
    url: '',
    navOrd: '',
    treeMenus: [],
    selectedTreeMenu: null
  },
  reducers: {
    setData(state, { payload }) {
      return { ...state, ...payload };
    },
    setSelectedTreeMenu(state, {payload: {selectedTreeMenu}}) {
      return { ...state, selectedTreeMenu};
    }
  },
  effects: {
    *fetch({ payload: {subModuleOrd} }, { call, put }) {
      let $handle='', $typeSpec='', title='', nIcon='', moduleType='', pagePath='', url='', navOrd='', treeMenus=[];
      console.log("fetch1 s", subModuleOrd);
      const subModule = yield call(bajaService.Ord, subModuleOrd);
      console.log("fetch2 s", subModule);
      if(subModule) {
        $handle = subModule.$handle;
        $typeSpec = subModule.getType().$typeSpec;
        title = subModule.getTitle();
        nIcon = subModule.getNIcon();
        navOrd = subModule.getNavOrd().toString();
        if(!$typeSpec.endsWith('ModuleGroup')) {
          const { $ordinal } =  subModule.getModuleType();
          moduleType = memoryUtils.MODULE_TYPE[$ordinal];
          pagePath = subModule.getPagePath();
          url = subModule.getUrl();
          if(moduleType === 'HISTORY'||moduleType === 'ALARM'||moduleType === 'TREE_MENU'||moduleType === 'PAGE') {
            const treeMenuRoot = yield call(bajaService.Ord, navOrd+'/treeMenuRoot');
            if(treeMenuRoot) {
              memoryUtils.TREEMENU_MODE = {};
              memoryUtils.TREEMENUITEM_TYPE = {};
              memoryUtils.ENERGYCATEGORY_TYPE = {};
              const treeMenuMode = treeMenuRoot.getTreeMenuModeExpose();
              const obj = JSON.parse(treeMenuMode);
              for(let key in obj) {
                memoryUtils.TREEMENU_MODE[obj[key]] = key;
              }
              const mode =  memoryUtils.TREEMENU_MODE[treeMenuRoot.getMode().$ordinal];
              treeMenus = yield call(bajaService.fetchNavChildren, treeMenuRoot);
              console.log("fetch3 s", treeMenus);
              treeMenus = yield call(bajaService.fetchTreeMenus, {treeMenus, mode});
              console.log("fetch4 s", treeMenus);
              if(!treeMenus) {
                treeMenus = []
              }
            }
          }
        }
      }
      // let $handle='', $typeSpec='', title='', moduleType='Tree Menu', pagePath='/subSystem', navOrd='', treeMenus=[];
      // const subModule = yield call(bajaService.Ord, subModuleOrd);
      // if(subModule) {
      //   $handle = subModule.$handle;
      //   $typeSpec = subModule.getType().$typeSpec;
      //   title = subModule.getTitle();
      //   const { $ordinal } =  subModule.getModuleType();
      //   moduleType = MODULE_TYPE[$ordinal];
      //   pagePath = subModule.getPagePath();
      //   navOrd = subModule.getNavOrd().toString();
      //   if(moduleType === 'TREE_MENU'||moduleType === 'HISTORY'||moduleType === 'ALARM'||moduleType === 'REALTIME'||moduleType === 'USER') {
      //     const treeMenuRoot = yield call(bajaService.Ord, navOrd+'/treeMenuRoot');
      //     if(treeMenuRoot) {
      //       treeMenus = yield call(bajaService.fetchNavChildren, treeMenuRoot);
      //       if(treeMenus) {
      //         for(let i=0; i<treeMenus.length; i++) {
      //           const { $typeSpec } = treeMenus[i].getType();
      //           if($typeSpec === CORE_JAR_PREFIX + ":TreeMenuGroup") {
      //             const treeMenuItems = yield call(bajaService.fetchNavChildren, treeMenus[i]);
      //             treeMenus[i] = {
      //               $handle: treeMenus[i].$handle,
      //               $typeSpec: $typeSpec,
      //               title: treeMenus[i].getTitle(),
      //               icon: 'folder',
      //               ord: treeMenus[i].getNavOrd().toString(),
      //               key: treeMenus[i].$handle,
      //             };
      //             if(treeMenuItems) {
      //               treeMenus[i].children = [];
      //               for(let j=0; j<treeMenuItems.length; j++) {
      //                 const { $typeSpec } = treeMenuItems[j].getType();
      //                 if($typeSpec === CORE_JAR_PREFIX + ":TreeMenuGroup") {
      //                   const itemTreeMenuItems = yield call(bajaService.fetchNavChildren, treeMenuItems[j]);
      //                   const itemGroup = {
      //                     $handle: treeMenuItems[j].$handle,
      //                     $typeSpec: $typeSpec,
      //                     title: treeMenuItems[j].getTitle(),
      //                     icon: 'folder',
      //                     ord: treeMenuItems[j].getNavOrd().toString(),
      //                     key: treeMenuItems[j].$handle,
      //                   };
      //                   itemGroup.children = itemTreeMenuItems.map(subItem => {
      //                     const { $ordinal } =  subItem.getTreeMenuItemType();
      //                     return ({
      //                       $handle: subItem.$handle,
      //                       $typeSpec: subItem.getType().$typeSpec,
      //                       title: subItem.getTitle(),
      //                       icon: subItem.getNIcon(),
      //                       url: subItem.getUrl(),
      //                       pagePath: subItem.getPagePath(),
      //                       ord: subItem.getNavOrd().toString(),
      //                       treeMenuItemType: TREEMENUITEM_TYPE[$ordinal],
      //                       key: subItem.$handle,
      //                     })
      //                   });
      //                   treeMenus[i].children.push(itemGroup);
      //                 } else if($typeSpec.endsWith("TreeMenuItem")) {
      //                   const { $ordinal } =  treeMenuItems[j].getTreeMenuItemType();
      //                   treeMenus[i].children.push({
      //                     $handle: treeMenuItems[j].$handle,
      //                     $typeSpec: treeMenuItems[j].getType().$typeSpec,
      //                     title: treeMenuItems[j].getTitle(),
      //                     icon: treeMenuItems[j].getNIcon(),
      //                     url: treeMenuItems[j].getUrl(),
      //                     pagePath: treeMenuItems[j].getPagePath(),
      //                     ord: treeMenuItems[j].getNavOrd().toString(),
      //                     treeMenuItemType: TREEMENUITEM_TYPE[$ordinal],
      //                     key: treeMenuItems[j].$handle,
      //                   });
      //                 }
      //               }
      //             }
      //           } else if($typeSpec.endsWith("TreeMenuItem")) {
      //             const { $ordinal } =  treeMenus[i].getTreeMenuItemType();
      //             treeMenus[i] = {
      //               $handle: treeMenus[i].$handle,
      //               $typeSpec: $typeSpec,
      //               title: treeMenus[i].getTitle(),
      //               icon: treeMenus[i].getNIcon(),
      //               url: treeMenus[i].getUrl(),
      //               pagePath: treeMenus[i].getPagePath(),
      //               ord: treeMenus[i].getNavOrd().toString(),
      //               treeMenuItemType: TREEMENUITEM_TYPE[$ordinal],
      //               key: treeMenus[i].$handle,
      //             }
      //           }
      //         }
      //       } else {
      //         treeMenus = []
      //       }
      //     }
      //   }
      // }
      yield put({ type: 'setData', payload: {
          $handle, $typeSpec, title, nIcon, moduleType, pagePath, url, navOrd, treeMenus, selectedTreeMenu:null
        }});
    },
    *clearAll({ _ }, { put }) {
      yield put({ type: 'setData', payload: {
          $handle: '',
          $typeSpec: CORE_JAR_PREFIX + ':CommonModule',
          title: '',
          moduleType: 'Tree Menu',
          pagePath: '/subSystem',
          url:'',
          navOrd: '',
          treeMenus: [],
          selectedTreeMenu: null
        }});
    },
    *selectTreeMenu({ payload: {selectedTreeMenu} }, { put }) {
      yield put({ type: 'setSelectedTreeMenu', payload: {selectedTreeMenu}});
    }
  }
}
